#include "j_additem.h"
#include "ui_j_additem.h"

J_AddItem::J_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::J_AddItem)
{
    ui->setupUi(this);

    table=TD_JOURNAL;

    updateCategory();
    updateRemindTrigger();
}

J_AddItem::~J_AddItem()
{
    delete ui;
}

void J_AddItem::updateCategory()
{
    if(ui->categories->count() != 0)
        ui->categories->clear();

    QStringList items=sp_getCategory(table);
    if(!items.isEmpty())
        ui->categories->addItems(items);
    else
    {
        ui->categories->addItem(CAT_JOURNAL);
        ui->categories->addItem(CAT_SCHEDULE);
        ui->categories->addItem(CAT_MEMO);
    }

    ui->categories->addItem(MANUAL_INPUT);
    ui->categories->setCurrentText(CAT_JOURNAL);
}

void J_AddItem::updateRemindTrigger()
{
    if(!remindList.isEmpty())
        remindList.clear();

    if(!remindMap.isEmpty())
        remindMap.clear();

    QSqlQuery query;
    query.exec(QString("select label,detail from %1 where topic='Remind'")
               .arg(TD_TRIGGER));

    while(query.next())
    {
        QString label=query.value(0).toString();
        QString detail=query.value(1).toString();
        if(label.isEmpty() || detail.isEmpty())
            continue;

        remindList.append(label);
        remindMap.insert(label,detail);
    }
}

void J_AddItem::on_categories_currentTextChanged(const QString &text)
{
    if(text == MANUAL_INPUT)
    {
        ui->category->clear();
        ui->category->setEnabled(true);
    }
    else
    {
        ui->category->setText(text);
        ui->category->setEnabled(false);
    }
}

bool J_AddItem::addItem(const QString &category,
                        const QString &detail,
                        const QString &addition)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (month_,date_,category,detail,addition) values (?,?,?,?,?)")
                  .arg(table));
    query.addBindValue(CurrentMonth);
    query.addBindValue(Today);
    query.addBindValue(category);
    query.addBindValue(detail);
    query.addBindValue(addition);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void J_AddItem::checkRemind(const QString &detail)
{
    if(remindList.isEmpty() || remindMap.isEmpty())
        return;

    QString remind;
    foreach (QString item, remindList) {
        if(detail.contains(item))
            remind.append("\n" + remindMap.value(item));
    }

    if(!remind.isEmpty())
        QMessageBox::information(this,"友情提示",remind);
}

void J_AddItem::on_btn_submit_clicked()
{
    QString category=ui->category->text().trimmed();
    if(category.isEmpty())
        category=CAT_JOURNAL;

    QString detail=ui->detail->toPlainText().trimmed();
    if(detail.isEmpty())
    {
        MESSAGE_DETAIL_EMPTY
    }

    QString remark=ui->remark->text().trimmed();
    if(addItem(category,detail,remark))
    {
        checkRemind(detail);
        accept();
    }
    else
        MESSAGE_CANNOT_SUBMIT
}
